package com.cmhk.zmail


import grails.converters.JSON

class MailController {
    MailService mailService

    def index(String folder) {
        redirect action: 'list'
    }

    def list(String folder){
        def folderList = mailService.listFolder()
        def mailData = mailService.getMailData(folder ?: 'INBOX')
        [folderList: folderList, mailData: mailData]
    }

    def getMailData(String folder, Integer startNum, Integer endNum) {

        def mailData = mailService.getMailData(folder, startNum, endNum)
        render mailData as JSON
    }

    def getMailCount(String folder){
        def result  = [messageCount:mailService.getMailCountByFolder(folder)]
        render result as JSON
    }

    def show(String folder,Long uid){
        def mailContent = mailService.getMailMessageByUid(folder,uid)
//        render mailContent.html
//        println mailContent.text
        [mailContent:mailContent]
    }

    def mailBodyHtml(String folder,Long uid){
        def mailContent = mailService.getMailMessageByUid(folder,uid)
        render mailContent.html?:(mailContent.text?:"")
    }


}
